package com.xq.sboot;

import com.xq.sboot.servlet.ManualFilter;
import com.xq.sboot.servlet.ManualListener;
import com.xq.sboot.servlet.ManualServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.servlet.http.HttpServlet;
import java.util.EventListener;

/**
 * 功能说明:符合Servlet规范的Bean配置类.
 * <p>创建时间:2018-12-14 10:08</p>
 *
 * @author <a href="https://gitee.com/gkq/microservice">郭凯强</a>
 * @since 1.0.0
 */
@Configuration
public class ServletConfig {
    @Bean
    public ServletRegistrationBean<HttpServlet> manualServletBean() {
        ServletRegistrationBean<HttpServlet> bean = new ServletRegistrationBean<>();
        bean.setServlet(new ManualServlet());
        bean.addUrlMappings("/manual/servlet"); //访问路径
        bean.setLoadOnStartup(2);
        bean.setOrder(1); //设置Bean的启动顺序, 与onStartup方法没有任何关系
        return bean;
    }

    @Bean
    public FilterRegistrationBean<Filter> manualFilterBean() {
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new ManualFilter());
        bean.addUrlPatterns("/*");
        bean.setName("session-filter");
        return bean;
    }

    @Bean
    public ServletListenerRegistrationBean<EventListener> manualListenerBean() {
        ServletListenerRegistrationBean<EventListener> bean = new ServletListenerRegistrationBean<>();
        bean.setListener(new ManualListener());
        return bean;
    }
}
